package com.android.jixin.channel.presenter.impl;

import android.app.Activity;
import android.util.Log;

import com.android.jixin.channel.MainActivity;
import com.android.jixin.channel.base.RetrofitService;
import com.android.jixin.channel.bean.Contributor;
import com.android.jixin.channel.presenter.IMainPresenter;
import com.android.jixin.channel.view.IMainView;
import com.android.library.base.BasePresenter;
import com.trello.rxlifecycle.ActivityEvent;

import java.util.List;

import javax.inject.Inject;

import rx.Subscriber;
import rx.schedulers.Schedulers;

/**
 * Author:  ljo_h
 * Date:    2016/8/7
 * Description:
 */
public class MainPresenterImpl extends BasePresenter<IMainView> implements IMainPresenter {

    private RetrofitService mService;
    private MainActivity mActivity;

    @Inject
    public MainPresenterImpl(RetrofitService service, Activity activity){
        mService = service;
        mActivity = (MainActivity) activity;
    }

    @Override
    public void getData() {
        mService.contributors("square","retrofit")
                .subscribeOn(Schedulers.io())
                .compose(mView.<List<Contributor>>applySchedulers())
                .compose(mActivity.<List<Contributor>>bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe(new Subscriber<List<Contributor>>() {
                    @Override
                    public void onCompleted() {
                        mView.showMsg("complete");
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(List<Contributor> contributors) {
                        for (Contributor c:contributors){
                            Log.i("TAG","RxJava-->"+c.getLogin()+"  "+c.getId()+"  "+c.getContributions());
                        }
                    }
                });
    }

}
